package bat.csvimport;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import com.csv.CsvUtil;

import csv.imp.CsvFileImportBatchBase;
import db.beans.M_CSVImportInterfaceBean;
import db.tables.T_ProductTable;

public class ProductCsvFileImport extends CsvFileImportBatchBase{
	private Map<Long, Long> categoryMap = null;

	public static void main(String[] args){

		try {

			CsvFileImport bat = new CsvFileImport();

			bat.startImport(args);

		} catch (Exception e){

			System.out.println(CsvUtil.getStringStackTrace(e));

			System.exit(1);
		}

		System.exit(0);
	}

	private void initCategoryMap() throws Exception{
		M_CategoryMappingBean[] arrCategoryMapping = M_CategoryMappingTable.selectAll(conn_);
		if (arrCategoryMapping != null && arrCategoryMapping.length >0){
			categoryMap = new HashMap<Long, Long>();
			for (M_CategoryMappingBean objCategoryMampping : arrCategoryMapping){
				categoryMap.put(new Long(objCategoryMampping.getWebsiteCategoryCode()), new Long(objCategoryMampping.getCategoryCode()));
			}
		}
	}

	protected String getReplaceValues(Connection conn, M_CSVImportInterfaceBean objImportIF, String value) throws Exception{
		String strReturnValue = "";
		if (objImportIF.getTargetColName().equalsIgnoreCase(T_ProductTable.COL_CATEGORY_ID)){
			if (categoryMap == null){
				initCategoryMap();
			} else {
				Long lValue = categoryMap.get(new Long(value));
				if (lValue != null){
					strReturnValue = String.valueOf(lValue);
				}
			}
		}
		return strReturnValue;
	}
}
